/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;

/**
 * BaseAbility
 *
 * @since 2021-07-14
 */
public abstract class BaseAbility extends Ability {
    protected static final int COLOR_PRIMARY = 0xFF0093FF;
    protected static final int COLOR_ACCENT = 0Xff006BBA;
    protected static final int COLOR_TRA = 0X00000000;
    protected Text toolbarTitle;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_base_ability);
        getWindow().setStatusBarColor(COLOR_PRIMARY);
        toolbarTitle = (Text) findComponentById(ResourceTable.Id_toolbar_title);
        findComponentById(ResourceTable.Id_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        DirectionalLayout root = (DirectionalLayout) findComponentById(ResourceTable.Id_root);
        root.addComponent(LayoutScatter.getInstance(this).parse(contentLayoutRes(), null, false));
    }

    abstract int contentLayoutRes();

    public CommonDialog initdialog(Component mComponent, Ability ability) {
        CommonDialog mCommonDialog = new CommonDialog(ability);
        mCommonDialog.setAutoClosable(false);
        mCommonDialog.setContentCustomComponent(mComponent);
        mCommonDialog.setTransparent(true);
        return mCommonDialog;
    }

    /**
     * 通过资源ID获取位图对象
     *
     * @param resId resId
     * @return return
     */
    public PixelMap getPixelMap(int resId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
